﻿using ValidationFramework.Extensions;
using System;
using ValidationFramework.Reflection;
using ValidationFramework.Configuration.Xml;
using ValidationFramework.Configuration;

namespace ValidationFramework.Specifications
{
	public class IsInRangeParser : ISpecificationXmlParser
	{
		public ISpecification GetSpecification(System.Xml.XmlElement SpecificationElement, RuleContext context)
		{
            // infer generic type
			var typeParam = context.TypeToValidate;

			Guard.ArgumentNotNull(SpecificationElement, "SpecificationElement");

            var minimum = SpecificationElement.GetAttributeWithConversion(typeParam, "minimum");
            var maximum = SpecificationElement.GetAttributeWithConversion(typeParam, "maximum");

            var Specification = (IIsInRangeSpecification)GenericTypeFactory.CreateInstance(typeof(IsInRangeSpec<>), typeParam, minimum, maximum);

			var equalsMinimumIsValid = SpecificationElement.GetAttribute<bool>("equalsMinimumIsValid", true);
            Specification.EqualsMinimumIsValid = equalsMinimumIsValid;
			var equalsMaximumIsValid = SpecificationElement.GetAttribute<bool>("equalsMaximumIsValid", true);

            Specification.EqualsMaximumIsValid = equalsMaximumIsValid;

            return Specification;
		}
	}
}
